import json

from rest_framework.views import APIView
from luffyapi.apps.home.utils.geetest import GeeTestLib
from rest_framework.response import Response
from luffyapi.apps.home.utils.captcha_verify import verify
from rest_framework import status
from django.conf import settings


class CaptchaView(APIView):

    def get(self, request):
        gt = GeeTestLib(settings.GEE_TEST["gee_test_access_id"], settings.GEE_TEST["gee_test_access_key"])
        gt.pre_process()
        # 设置 geetest session, 用于是否启用滑动验证码向 geetest 发起远程验证, 如果取不到的话只是对本地轨迹进行校验
        # self.request.session[gt.GT_STATUS_SESSION_KEY] = status
        # request.session["user_id"] = user_id
        response_str = gt.get_response_str()
        response_str = json.loads(response_str)

        return Response({"error_no": 0, "data": response_str})
    def post(self,request):
        is_valid = verify(request.data)
        res = {'check': is_valid}
        if is_valid:
            return Response(res)
        else:
            return Response(res,status=status.HTTP_403_FORBIDDEN)

